<?php

  function modSefRef ( $osModName = null ) {
    global $osMod;
    if ( !$osModName ) { $osModName = $osMod; };
    return $osModName . '.php';
  }; // modSefRef

  // --------------------------------------------------------------------------------------------------------------
  // remove parameters from a request url
  function remove_param( $param_str, $remove ) {
    $param_str = preg_replace( '/\&|\?/', "{SEP}", $param_str );
    $param_arr = split( '{SEP}', $param_str );
    $final_val = null;
    foreach ( $param_arr as $param ) {
      $pass = 1;
      foreach ( $remove as $key ) {
        if ( preg_match( "/^$key/", $param ) ) { $pass = 0; break; }
      }
      if ( $pass ) { $final_val .= $param . '&'; };
    }
    $final_val = preg_replace( '/\&+$/', '', $final_val );
    return $final_val;
  }; // remove_params

  // --------------------------------------------------------------------------------------------------------------
  // The HTML href link wrapper function
  function tep_href_link($page = '', $parameters = '', $connection = 'NONSSL', $add_session_id = true, $search_engine_safe = true) {
  global $kill_sid, $_GET;
    global $request_type, $session_started, $SID;

    if (!tep_not_null($page)) {
      die('</td></tr></table></td></tr></table><br><br><font color="#ff0000"><b>Error!</b></font><br><br><b>Unable to determine the page link!<br><br>');
    }
if (isset($_GET['language']) && $kill_sid) {
      $l = ereg('[&\?/]?language[=/][a-z][a-z]', $parameters, $m);
      if ($l) {
        $parameters = ereg_replace("[&\?/]?language[=/][a-z][a-z]", "", $parameters);
        $_GET['language'] = substr($m[0],-2);
      }
      if (tep_not_null($parameters)) {
        $parameters .= "&amp;language=" . $_GET['language'];
      } else {
        $parameters = "language=" . $_GET['language'];
      }
    }
    if ($connection == '') {
if (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] == '' || $_SERVER['HTTPS'] == 'off')
$link = HTTP_SERVER . DIR_WS_HTTP_CATALOG;
else
$link = HTTPS_SERVER . DIR_WS_HTTPS_CATALAG;
} elseif ($connection == 'NONSSL') {
$link = HTTP_SERVER . DIR_WS_HTTP_CATALOG;
} elseif ($connection == 'SSL') {
if (ENABLE_SSL == 'true') {
$link = HTTPS_SERVER . DIR_WS_HTTPS_CATALOG;
} else {
$link = HTTP_SERVER . DIR_WS_HTTP_CATALOG;
}
} else {
die('</td></tr></table></td></tr></table><br><br><font color="#ff0000"><b>Error!</b></font><br><br><b>Unable to determine connection method on a link!');
}

    // -- webMS Modification --
    $page_id = preg_replace( '/\.php/', '', $page);
    if ( preg_match( '/popup/', $page_id ) || preg_match( '/popup/', $parameters ) ) {
      $page_ud = preg_replace( '/^[\w_]+\./', 'index2.', $page);
      $dosefecd = 'false';
    } else {
      $page_ud = preg_replace( '/^[\w_]+\./', 'index.', $page);
      $dosefecd = 'true';
    }
    $link .= $page_ud;
    $parameters = remove_param( $parameters, array('option','osMod','ecjcID', 'task') );

    // Debug Line
    // echo "PAGE[ $page ] PAGE_ID [ $page_id ] PAGE_UD [ $page_id ] LINK [ $link ] PARAMETERS [ $parameters ]";


global $Itemid, $database;
if($Itemid == ''){
$database->setQuery("
		SELECT id
		FROM #__menu
		WHERE link = 'index.php?option=com_oscommerce' and published = 1
		LIMIT 1");
	$Itemid = $database->loadResult();
}

//ok itemid needs to be the store itemid? so best is to get the latest osc menu item right?







    if ( $parameters != 'NONE' ) {
      if (preg_match('/\?/',$page)){
        if ($parameters == '') {
          $link .= '&amp;option=com_oscommerce&amp;osMod=' . $page_id. '&amp;Itemid='. $Itemid;
        } else {
          $link .= '&amp;option=com_oscommerce&amp;osMod=' . $page_id  . '&amp;Itemid='. $Itemid .'&amp;' . $parameters  ;
        }
      } else {
        if ($parameters == '') {
          $link .= '?option=com_oscommerce&amp;osMod=' . $page_id. '&amp;Itemid='. $Itemid;
        } else {
          $link .= '?option=com_oscommerce&amp;osMod=' . $page_id . '&amp;Itemid='. $Itemid .'&amp;' . $parameters ;
        }
      }
    }
    
	
    $separator = '&amp;';

    while ( (substr($link, -1) == '&') || (substr($link, -1) == '?') ) $link = substr($link, 0, -1);

    // Add the session ID when moving from different HTTP and HTTPS servers, or when SID is defined
    if ( ($add_session_id == true) && ($session_started == true) && (SESSION_FORCE_COOKIE_USE == 'False') ) {
      if (tep_not_null($SID)) {
        $_sid = $SID;
      } elseif ( ( ($request_type == 'NONSSL') && ($connection == 'SSL') && (ENABLE_SSL == 'true') ) || ( ($request_type == 'SSL') && ($connection == 'NONSSL') ) ) {
        if (HTTP_COOKIE_DOMAIN != HTTPS_COOKIE_DOMAIN) {
          $_sid = tep_session_name() . '=' . tep_session_id();
        }
      }
    }
if (isset($_sid) && ( !$kill_sid ) ) {
      $link .= $separator . tep_output_string($_sid);
    }
    $link = str_replace('&amp;', '&', $link);
    $link = str_replace('&&', '&', $link);
    $link = str_replace('&', '&amp;', $link);
    
    if ( (SEARCH_ENGINE_FRIENDLY_URLS == 'true') && ($search_engine_safe == true)) {
   
     $link = sefRelToAbs($link);
  
    }
    
    /*testing block only*/
    //$link = HTTP_SERVER . DIR_WS_HTTP_CATALOG;
 //   if(class_exists('jroute')){
  //  $link2 = str_replace(HTTP_SERVER . DIR_WS_HTTP_CATALOG , '', $link);
  //  $link2 = str_replace(HTTPS_SERVER . DIR_WS_HTTPS_CATALAG , '', $link2);
   // echo $link2;
//   $link2 = JRoute::_($link2);
	//$link2 = DIR_WS_CATALOG . $link;
  // return $link2;
  // }else{
    return $link;
  //  }
  }

  // --------------------------------------------------------------------------------------------------------------
  // The HTML image wrapper function
function tep_image($src, $alt = '', $width = '', $height = '', $params = '') {
 // if no file exists display the 'no image' file
//echo $src;
//echo basename($src);
if ( (empty($src) || ($src == DIR_WS_IMAGES)) && (IMAGE_REQUIRED == 'false')) {
      //return false;
      return '<img src="'.DIR_WS_IMAGES.'no_picture.gif" border="0" />';
    }


//backward compliancy
if (THUMBNAIL_USE_RESAMPLING == 'true' ) {

    // Set default image variable and code
    $image = '<img src="' . $src . '"';
    //echo $image;
    //$image = '<img src="' .tep_output_string($src) .'"';

    // Don't calculate if the image is set to a "%" width
    if (strstr($width,'%') == false || strstr($height,'%') == false) { 
        $dont_calculate = 0; 
    } else {
        $dont_calculate = 1;    
    }

    // Dont calculate if a pixel image is being passed (hope you dont have pixels for sale)
    if (!strstr($image, 'pixel')) {
        $dont_calculate = 0;
    } else {
        $dont_calculate = 1;
    }

    // Do we calculate the image size?
    if (CONFIG_CALCULATE_IMAGE_SIZE && !$dont_calculate) {

        // Get the image's information
        //echo (DIR_FS_HTTP_ROOT.$src);
        if ($image_size = @getimagesize(HTTP_SERVER.$src)) { 

            $ratio = $image_size[1] / $image_size[0];

            // Set the width and height to the proper ratio
            if (!$width && $height) {
                $ratio = $height / $image_size[1];
                $width = intval($image_size[0] * $ratio); 
            } elseif ($width && !$height) {
                $ratio = $width / $image_size[0];
                $height = intval($image_size[1] * $ratio); 
            } elseif (!$width && !$height) {
                $width = $image_size[0];
                $height = $image_size[1]; 
            }

            // Scale the image if not the original size
            if ($image_size[0] != $width || $image_size[1] != $height) {
                $rx = $image_size[0] / $width;
                $ry = $image_size[1] / $height;

                if ($rx < $ry) {
                    $width = intval($height / $ratio);
                } else {
                    $height = intval($width * $ratio);
                }

               $image = '<img src="'. tep_href_link('product_thumb.php', 'popup=popup&img='.$src.'&w=' . tep_output_string($width) . '&h=' . tep_output_string($height) . '&uri='.HTTP_SERVER).'"';
            }

        }
        //else{
        /// WE NEED TO FIND ANOTHER WAY TO GENERATE IMAGE PREPORTIONS HERE>>>>
        //}
    }

    // Add remaining image parameters if they exist
    if ($width) {
        $image .= ' width="' . tep_output_string($width) . '"'; 
    } 

    if ($height) { 
        $image .= ' height="' . tep_output_string($height) . '"'; 
    }     

    if (tep_not_null($params)) $image .= ' ' . $params;

    $image .= ' border="0" alt="' . tep_output_string($alt) . '"';

    if (tep_not_null($alt)) {
        $image .= ' title="' . tep_output_string($alt) . '"';
    }

    $image .= ' />';   
    
    }else{
    //use default osc functionality
    $image = '<img src="'. $src . '" border="0" alt="' . tep_output_string($alt) . '"';

    if (tep_not_null($alt)) {
      $image .= ' title=" ' . tep_output_string($alt) . ' "';
    }

    if ( (CONFIG_CALCULATE_IMAGE_SIZE == 'true') && (empty($width) || empty($height)) ) {
      if ($image_size = @getimagesize($src)) {
        if (empty($width) && tep_not_null($height)) {
          $ratio = $height / $image_size[1];
          $width = $image_size[0] * $ratio;
        } elseif (tep_not_null($width) && empty($height)) {
          $ratio = $width / $image_size[0];
          $height = $image_size[1] * $ratio;
        } elseif (empty($width) && empty($height)) {
          $width = $image_size[0];
          $height = $image_size[1];
        }
        }
        
     
    }

    if (tep_not_null($width) && tep_not_null($height)) {
      $image .= ' width="' . tep_output_string($width) . '" height="' . tep_output_string($height) . '"';
    }elseif(tep_not_null($height) && empty($width)){
      $image .= ' height="' . tep_output_string($height) . '"';
    }elseif(tep_not_null($width) && empty($height)){
      $image .= ' width="' . tep_output_string($width) . '"';
     }

    if (tep_not_null($parameters)) $image .= ' ' . $parameters;

    $image .= ' />';
    }

    return $image; 
    
}

  // --------------------------------------------------------------------------------------------------------------
 
  //start 118rc2
  
  // to move to db
  //define ('USE_CSS_BUTTONS', 'no');
  // end move to db
function tep_image_submit($image, $alt = '', $parameters = '') {
    global $language;
    if (strtolower(USE_CSS_BUTTONS) == 'yes' && strlen($alt)<30) return ecjcCssButton($image, $alt, 'submit', $sec_class /*, $parameters = ''*/ );

    $image_submit = '<input type="image" src="' . tep_output_string(WS_TEMPLATES_STYLE . E_TEMPLATE_FOLDER . '/buttons/' . $language .'/'. $image) . '" border="0" alt="' . tep_output_string($alt) . '"';

    if (tep_not_null($alt)) $image_submit .= ' title=" ' . tep_output_string($alt) . ' "';

    if (tep_not_null($parameters)) $image_submit .= ' ' . $parameters;

    $image_submit .= '>';

    return $image_submit;
  }

  // --------------------------------------------------------------------------------------------------------------
  // Output a function button in the selected language
  function tep_image_button($image, $alt = '', $parameters = '') {
    global $language;
if (strtolower(USE_CSS_BUTTONS) == 'yes') return ecjcCssButton($image, $alt, 'button', $sec_class, $parameters = '');

   // return tep_image('components/com_ecjc_lang/templates/' . E_TEMPLATE_FOLDER . '/buttons/' . $language .'/'. $image, $alt, '', '', $parameters);
   return '<img src="' . tep_output_string(WS_TEMPLATES_STYLE . E_TEMPLATE_FOLDER . '/buttons/' . $language .'/'. $image) . '" border="0" alt="'. $alt . '" />';
  }
  // end 118rc2
    
  // --------------------------------------------------------------------------------------------------------------
  function tep_image_submit_ECJ($image, $value = '-AltValue-', $parameters
= '', $clickaction) {
return '<a href="'.$clickaction.'">'.tep_image_button($image, $value, $parameters).'</a>';
}

function ecjcCssButton($image = '', $text, $type, $sec_class = '', $parameters = '') {

    // automatic width setting depending on the number of characters
    $min_width = 80; // this is the minimum button width, change the value as you like
    $character_width = 6.5; // change this value depending on font size!
    // end settings
    // added html_entity_decode function to prevent html special chars to be counted as multiple characters (like &amp;)
    $width = strlen(html_entity_decode($text)) * $character_width;
    $width = (int)$width;
    if ($width < $min_width) $width = $min_width;
    $style = ' style="width: ' . $width . 'px;"';
    // if no secondary class is set use the image name for the sec_class
    if (empty($sec_class)) $sec_class = basename($image, '.gif');
    if(!empty($sec_class))$sec_class = ' ' . $sec_class;
    if(!empty($parameters))$parameters = ' ' . $parameters;
    $mouse_out_class  = 'cssButton' . $sec_class;
    $mouse_over_class = 'cssButtonHover' . $sec_class . $sec_class . 'Hover';
    // javascript to set different classes on mouseover and mouseout: enables hover effect on the buttons
    // (pure css hovers on non link elements do work work in every browser)
    $css_button_js .=  'onmouseover="this.className=\''. $mouse_over_class . '\'" onmouseout="this.className=\'' . $mouse_out_class . '\'"';

    if ($type == 'submit'){
// form input button
   $css_button = '<input class="' . $mouse_out_class . '" ' . $css_button_js . ' type="submit" value="' .$text . '"' . $parameters . $style . ' />';
    }

    if ($type=='button'){
// link button
   $css_button = '<span class="' . $mouse_out_class . '" ' . $css_button_js . $style . ' >&nbsp;' . $text . '&nbsp;</span>'; // add $parameters ???
    }
    return $css_button;
  }

  
    function tep_draw_separator($image = 'pixel_black.gif', $width = '100%', $height = '1') {
    
    return tep_image( DIR_WS_IMAGES . $image, '', $width, $height);
  }
  // Output a form
  function tep_draw_form($name, $action, $method = 'post', $parameters = '') {
$action = str_replace('&amp;', '&', $action);
$parameters = str_replace('&amp;', '&', $parameters);
    $action_url = preg_replace( '/\?.*/','',$action);
    $action_par = preg_replace( '/.*\?/','',$action);
//k itemid fuckup
if($name == 'checkout_payment' || $name == 'order'){
$action_url .= '?Itemid=' . $_REQUEST['Itemid'];
}

    $form = '<form name="' . tep_output_string($name) . '" action="' . tep_output_string($action_url) . '" method="' . tep_output_string($method) . '"';

    if (tep_not_null($parameters)) $form .= ' ' . $parameters;

    $form .= '>';

    if ($action_par != '') {
      $action_params = array();
      $action_params = split('&',$action_par);
      foreach ($action_params AS $this_par) {
        list($key, $value) = split('=',$this_par);
        
        $form .= tep_draw_hidden_field( $key, $value );
      };
    }

    return $form;
  }

  // --------------------------------------------------------------------------------------------------------------
  // Output a form input field
  function tep_draw_input_field($name, $value = '', $parameters = '', $type = 'text', $reinsert_value = true) {
    global $_GET, $_POST;
    $field = '<input type="' . tep_output_string($type) . '" name="' . tep_output_string($name) . '"';

    if ( ($reinsert_value == true) && ( (isset($_GET[$name]) && is_string($_GET[$name])) || (isset($_POST[$name]) && is_string($_POST[$name])) ) ) {
      if (isset($_GET[$name]) && is_string($_GET[$name])) {
        $value = stripslashes($_GET[$name]);
      } elseif (isset($_POST[$name]) && is_string($_POST[$name])) {
        $value = stripslashes($_POST[$name]);
      }
    }

    if (tep_not_null($value)) {
      $field .= ' value="' . tep_output_string($value) . '"';
    }

    if (tep_not_null($parameters)) $field .= ' ' . $parameters;

    $field .= '>';

    return $field;
  }

  // --------------------------------------------------------------------------------------------------------------
  // Output a form password field
  function tep_draw_password_field($name, $value = '', $parameters = 'maxlength="40"') {
    return tep_draw_input_field($name, $value, $parameters, 'password', false);
  }

  // --------------------------------------------------------------------------------------------------------------
  // Output a selection field - alias function for tep_draw_checkbox_field() and tep_draw_radio_field()
  function tep_draw_selection_field($name, $type, $value = '', $checked = false, $parameters = '') {
    global $_GET, $_POST;
    $selection = '<input type="' . tep_output_string($type) . '" name="' . tep_output_string($name) . '"';

    if (tep_not_null($value)) $selection .= ' value="' . tep_output_string($value) . '"';

    if ( ($checked == true) || (isset($_GET[$name]) && is_string($_GET[$name]) && (($_GET[$name] == 'on') || (stripslashes($_GET[$name]) == $value))) || (isset($_POST[$name]) && is_string($_POST[$name]) && (($_POS[$name] == 'on') || (stripslashes($_POST[$name]) == $value))) ) {
      $selection .= ' CHECKED';
    }

    if (tep_not_null($parameters)) $selection .= ' ' . $parameters;

    $selection .= '>';

    return $selection;
  }

  // --------------------------------------------------------------------------------------------------------------
  // Output a form checkbox field
  function tep_draw_checkbox_field($name, $value = '', $checked = false, $parameters = '') {
    return tep_draw_selection_field($name, 'checkbox', $value, $checked, $parameters);
  }

  // --------------------------------------------------------------------------------------------------------------
  // Output a form radio field
  function tep_draw_radio_field($name, $value = '', $checked = false, $parameters = '') {
    return tep_draw_selection_field($name, 'radio', $value, $checked, $parameters);
  }

  // --------------------------------------------------------------------------------------------------------------
  // Output a form textarea field
  function tep_draw_textarea_field($name, $wrap, $width, $height, $text = '', $parameters = '', $reinsert_value = true) {
  global $_GET, $_POST;
    $field = '<textarea name="' . tep_output_string($name) . '" wrap="' . tep_output_string($wrap) . '" cols="' . tep_output_string($width) . '" rows="' . tep_output_string($height) . '"';

    if (tep_not_null($parameters)) $field .= ' ' . $parameters;

    $field .= '>';

    // if ( (isset($GLOBALS[$name])) && ($reinsert_value == true) ) {
	if ( ($reinsert_value == true) && ( (isset($_GET[$name]) && is_string($_GET[$name])) || (isset($_POST[$name]) && is_string($_POST[$name])) ) ) {
	   if (isset($_GET[$name]) && is_string($_GET[$name])) {
	      $field .= tep_output_string_protected(stripslashes($_GET[$name]));
	   } elseif (isset($_POST[$name]) && is_string($_POST[$name])) {
	      $field .= tep_output_string_protected(stripslashes($_POST[$name]));
	   }

 // $field .= tep_output_string_protected(stripslashes($GLOBALS[$name]));

} elseif (tep_not_null($text)) {

  $field .= tep_output_string_protected($text);

}



    $field .= '</textarea>';

    return $field;
  }

  // --------------------------------------------------------------------------------------------------------------
  // Output a form hidden field
  function tep_draw_hidden_field($name, $value = '', $parameters = '') {
    global $_GET, $_POST;
    $field = '<input type="hidden" name="' . tep_output_string($name) . '"';

    if (tep_not_null($value)) {
      $field .= ' value="' . tep_output_string($value) . '"';
    } elseif ( (isset($_GET[$name]) && is_string($_GET[$name])) || (isset($_POST[$name]) && is_string($_POST[$name])) ) {
	  if ( (isset($_GET[$name]) && is_string($_GET[$name])) ) {
      // $field .= ' value="' . tep_output_string(stripslashes($GLOBALS[$name])) . '"';
	  $field .= ' value="' . tep_output_string(stripslashes($_GET[$name])) . '"';
	} elseif ( (isset($_POST[$name]) && is_string($_POST[$name])) ) {
	  $field .= ' value="' . tep_output_string(stripslashes($_POST[$name])) . '"';
	  }
    }

    if (tep_not_null($parameters)) $field .= ' ' . $parameters;

    $field .= '>';

    return $field;
  }

  // --------------------------------------------------------------------------------------------------------------
  // Hide form elements
  function tep_hide_session_id() {
    global $session_started, $SID;

    if (($session_started == true) && tep_not_null($SID)) {
      return tep_draw_hidden_field(tep_session_name(), tep_session_id());
    }
  }

  // --------------------------------------------------------------------------------------------------------------
  // Output a form pull down menu
  function tep_draw_pull_down_menu($name, $values, $default = '', $parameters = '', $required = false) {
    global $_GET, $_POST;
    $field = '<select name="' . tep_output_string($name) . '"';

    if (tep_not_null($parameters)) $field .= ' ' . $parameters;

    $field .= '>';

    //if (empty($default) && isset($GLOBALS[$name])) $default = stripslashes($GLOBALS[$name]);
	if (empty($default) && ( (isset($_GET[$name]) && is_string($_GET[$name])) || (isset($_POST[$name]) && is_string($_POST[$name])) ) ) {
	   if (isset($_GET[$name]) && is_string($_GET[$name])) {
	     $default = stripslashes($_GET[$name]);
	   } elseif (isset($_POST[$name]) && is_string($_POST[$name])) {
	     $default = stripslashes($_POST[$name]);
	   }
	  }

    for ($i=0, $n=sizeof($values); $i<$n; $i++) {
      $field .= '<option value="' . tep_output_string($values[$i]['id']) . '"';
      if ($default == $values[$i]['id']) {
        $field .= ' SELECTED';
      }

      $field .= '>' . tep_output_string($values[$i]['text'], array('"' => '&quot;', '\'' => '&#039;', '<' => '&lt;', '>' => '&gt;')) . '</option>';
    }
    $field .= '</select>';

    if ($required == true) $field .= TEXT_FIELD_REQUIRED;

    return $field;
  }

  // --------------------------------------------------------------------------------------------------------------
  // Creates a pull-down list of countries
  function tep_get_country_list($name, $selected = '', $parameters = '') {
    $countries_array = array(array('id' => '', 'text' => PULL_DOWN_DEFAULT));
    $countries = tep_get_countries();

    for ($i=0, $n=sizeof($countries); $i<$n; $i++) {
      $countries_array[] = array('id' => $countries[$i]['countries_id'], 'text' => $countries[$i]['countries_name']);
    }

    return tep_draw_pull_down_menu($name, $countries_array, $selected, $parameters);
  }
  function tep_draw_file_field($name, $required = false) {
    $field = tep_draw_input_field($name, '', '', 'file',$required);
    //return tep_draw_input_field($name, $value, $parameters, 'password', false);

    return $field;
  }
  
  function tep_draw_pull_down_date($name='date', $day='', $month='', $year='', $full=false){
	if ($day=='') {$day=date('d');}
	if ($month=='') {$month=date('m');}
	if ($year=='') {$year=date('Y');}
	$named = $name . 'd';
	$namem = $name . 'm';
// days
for($i=1; $i<=31; $i++){
if(strlen($i)!= 2){
$j = '0' . $i;
} else {
$j = $i;
} 
$days[] = array('id' => $j, 'text' => $j); }

// months
for($i=1; $i<=12; $i++){
if(strlen($i)!= 2){
$j = '0' . $i;
} else {
$j = $i;
} 
$months[] = array('id' => $j, 'text' => $j); }

// years
for($i=date('Y')-1; $i<=date('Y')+2; $i++){
$j = $i;
if ($full) {
$y = $i - 2000;
if(strlen($y)!= 2){
$j = '0' . $y;
} else {
$j = $y;
} }
$years[] = array('id' => $i, 'text' => $j); }
$field='';
if(STORE_COUNTRY == 223 || STORE_COUNTRY == 224) {
$field .= tep_draw_pull_down_menu($named, $months, $month);
$field .= tep_draw_pull_down_menu($namem, $days, $day); }
else {
$field .= tep_draw_pull_down_menu($named, $days, $day);
$field .= tep_draw_pull_down_menu($namem, $months, $month); }
$field .= tep_draw_pull_down_menu($name, $years, $year);

return $field ;
	}
?>
